<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增项目计划')"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-plan-add">
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">训练包名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="trainName" id="trainName" required autocomplete="off">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label is-required" >年龄年级段：</label>
            <div class="col-sm-3">
                <select name="ageGrade" id="ageGrade" class="form-control" required
                        th:with="type=${@dict.getType('age_grade')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}">
                    </option>
                </select>
            </div>

            <label class="col-sm-2 control-label is-required">上级训练包：</label>
            <div class="col-sm-3">
                <select id="parentTrainId" name="parentTrainId" class="form-control">
                </select>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script type="text/javascript">
    // 字典
    var prefix = ctx + "business/trainPackage";
    $(function () {
        selectParentTrain();
    })

    function selectParentTrain(){
        var url = prefix + "/getParentTrainPackage" ;
        $.ajax({
            url: url,
            type: "get",
            processData: false,
            contentType: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $("#parentTrainId").find("option").remove();
                $("#parentTrainId").append("<option value=\"\" selected>无</option>");
                for(var i in data){
                    $("#parentTrainId").append("<option value="+data[i].id+">"+data[i].trainName+"</option>");
                }
            }
        })
    }
    $("#form-plan-add").validate({
        onkeyup: false,
        rules:{
            trainName:{
                remote: {
                    url: prefix + "/checkTrainNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "trainName": function() {
                            return $.common.trim($("#trainName").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            }
        },
        messages: {
            "trainName": {
                remote: "训练包名称已经存在"
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            add();
        }
    }
    function add() {
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/add",
            data: {
                "trainName": $("#trainName").val(),
                "parentTrainId": $("#parentTrainId").val(),
                "ageGrade":$("#ageGrade").val()
            },
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $.operate.successCallback(data);
            }
        });
    }
</script>
</body>
</html>
